/* 
*
* 当日实时能耗
*
 */

(function() {
    // var xAxis = [];
    // var averageData = [727,1555.5,2461.5,3314.5, 4242,4968.5,5370.5,6964,8147.5,9232,10315.5,11460,12679.5,13708,14857,15872,16981,17939.5,18846,19778,20714,21656,22392.5,23308.5];  // 平均数据
    // var realTimeDate = [];  // 今天实时数据
    // 每隔12min请求一次
    // getData();
    // setInterval(function(){
    //     getData();
    // },720000)
    /* function getData() {
        $.ajax({
                url: window.base_url + '/ccp/electryShow/showHourData',
                type: 'post',
                dataType: 'json',
                data: {
                    token: window.token,
                    time: getToday(),
                    parkId: 9,
                },
                success: function(res) {
                    // console.log(res)
                    if (res.retcode == 200) {
                        const data = res.data;
                        if(data && data.length) {
                            data.forEach(function(item){
                                xAxis.push( item.time.slice(-2) );
                                realTimeDate.push( Math.abs(item.takeValue) );
                            })
                            averageData = averageData.slice(0, xAxis.length);
                        }
                    }
                    // console.log(averageData)
                    var option = {
                        legend: {
                            data: [{ name: '平均', icon: 'rect' }, { name: '实时', icon: 'rect' }],
                            textStyle: {
                                color: 'rgba(255,255,255,0.8)'
                            },

                        },
                        tooltip: {
                            trigger: 'axis'
                        },
                        color: ['rgba(43,153,255,0.9)', 'rgba(43,153,255,0.4)'],
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: [
                            {
                                type: 'category',
                                boundaryGap: false,
                                data: xAxis,
                                axisLabel: {
                                    textStyle: {
                                        color: '#838687',
                                        fontSize: '10'
                                    },
                                    // interval: 0,
                                },
                            }
                        ],
                        yAxis: [
                            {
                                type: 'value',
                                splitLine: {
                                    show: false
                                },
                                axisLabel: {
                                    textStyle: {
                                        color: '#838687',
                                        fontSize: '10'
                                    },
                                    interval: 0,
                                },
                                lineStyle: {

                                }
                            },

                        ],
                        series: [
                            {
                                name: '平均',
                                type: 'line',
                                stack: '总量',
                                smooth: true,
                                areaStyle: {
                                    normal: {
                                        color: 'rgba(43,153,255,0.9)'
                                    }
                                },
                                data: averageData
                            },
                            {
                                name: '实时',
                                type: 'line',
                                stack: '总量',
                                style: {
                                    // stroke: 'rgba(255,255,255,0)'
                                },
                                areaStyle: {
                                    areaStyle: {
                                        normal: {
                                            color: 'rgba(43,153,255,0.4)'
                                        }
                                    },
                                },
                                smooth: true,
                                data: realTimeDate
                            },
                        ]
                    };
                    echarts.init(document.getElementById('energyConsumption')).setOption(option);
                },
                error: function(err) {
                    var option = {
                        legend: {
                            data: [{ name: '平均', icon: 'rect' }, { name: '实时', icon: 'rect' }],
                            textStyle: {
                                color: 'rgba(255,255,255,0.8)'
                            },

                        },
                        color: ['rgba(43,153,255,0.9)', 'rgba(43,153,255,0.4)'],
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: [
                            {
                                type: 'category',
                                boundaryGap: false,
                                data: xAxis,
                                axisLabel: {
                                    textStyle: {
                                        color: '#838687',
                                        fontSize: '10'
                                    },
                                    interval: 0,
                                },
                            }
                        ],
                        yAxis: [
                            {
                                type: 'value',
                                splitLine: {
                                    show: false
                                },
                                axisLabel: {
                                    textStyle: {
                                        color: '#838687',
                                        fontSize: '10'
                                    },
                                    interval: 0,
                                },
                                lineStyle: {

                                }
                            },

                        ],
                        series: [
                            {
                                name: '平均',
                                type: 'line',
                                stack: '总量',
                                smooth: true,
                                areaStyle: {
                                    normal: {
                                        color: 'rgba(43,153,255,0.9)'
                                    }
                                },
                                data: averageData
                            },
                            {
                                name: '实时',
                                type: 'line',
                                stack: '总量',
                                style: {
                                    // stroke: 'rgba(255,255,255,0)'
                                },
                                areaStyle: {
                                    areaStyle: {
                                        normal: {
                                            color: 'rgba(43,153,255,0.4)'
                                        }
                                    },
                                },
                                smooth: true,
                                data: realTimeDate
                            },
                        ]
                    };
                    echarts.init(document.getElementById('energyConsumption')).setOption(option);
                    console.log(err)
                }
            })
    }; */
    function getPreDay() {  // 获取前一天时间
        let myTime = new Date();
        let year = myTime.getFullYear();
        let month = myTime.getMonth() + 1;
        let day = myTime.getDate();
        let arr = [];
        day = day - 1  // 取昨天日期
        if (day == 0) {
            if ((month - 1) == 1 || (month - 1) == 3 || (month - 1) == 5 || (month - 1) == 7 || (month - 1) == 8 || (month - 1) == 10 || (month - 1) == 12 || (month - 1) == 0) {
                day = 31;
            } else if ((month - 1) == 2) {
                // 闰年 1种是 能被4整除且不能被100整除 2种 能被400整除
                if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                    day = 28;
                } else {
                    day = 29;
                }
            } else {
                day = 30;
            }
            month = (month - 1);
            if (month == 0) {
                month = 12;
                year = year - 1;
            }
            // console.log(month)
        }
        arr.push(year);
        arr.push(addZero(month));
        arr.push(addZero(day));
        return arr.join('-');
    };
    function addZero(n) {
        if (n < 10) {
            n = '0' + n
        }
        return n
    };
    function getToday() {  // 获取当天时间
        var myDate = new Date();
        var year = myDate.getFullYear();
        var mounth = myDate.getMonth() + 1;
        var date = myDate.getDate();
        var today = year +'-'+ addZero(mounth) +'-'+ addZero(date);
        return today
    }
    /* *
    *
    * 实时假数据
    * 
    *  */
    // x坐标
    // var xAxisMock = [
    //     '0','0:10', '0:20', '0:30', '0:40', '0:50',
    //     '1', '1:10', '1:20', '1:30', '1:40', '1:50',
    //     '2', '2:10', '2:20', '2:30', '2:40', '2:50',
    //     '3', '3:10', '3:20', '3:30', '3:40', '3:50',
    //     '4', '4:10', '4:20', '4:30', '4:40', '4:50',
    //     '5', '5:10', '5:20', '5:30', '5:40', '5:50',
    //     '6', '6:10', '6:20', '6:30', '6:40', '6:50',
    //     '7', '7:10', '7:20', '7:30', '7:40', '7:50',
    //     '8', '8:10', '8:20', '8:30', '8:40', '8:50',
    //     '9', '9:10', '9:20', '9:30', '9:40', '9:50',
    //     '10', '10:10', '10:20', '10:30', '10:40', '10:50',
    //     '11', '11:10', '11:20', '11:30', '11:40', '11:50',
    //     '12', '12:10', '12:20', '12:30', '12:40', '12:50',
    //     '13', '13:10', '13:20', '13:30', '13:40', '13:50',
    //     '14', '14:10', '14:20', '14:30', '14:40', '14:50',
    //     '15', '15:10', '15:20', '15:30', '15:40', '15:50',
    //     '16', '16:10', '16:20', '16:30', '16:40', '16:50',
    //     '17', '17:10', '17:20', '17:30', '17:40', '17:50',
    //     '18', '18:10', '18:20', '18:30', '18:40', '18:50',
    //     '19', '19:10', '19:20', '19:30', '19:40', '19:50',
    //     '20', '20:10', '20:20', '20:30', '20:40', '20:50',
    //     '21', '21:10', '21:20', '21:30', '21:40', '21:50',
    //     '22', '22:10', '22:20', '22:30', '22:40', '22:50',
    //     '23', '23:10', '23:20', '23:30', '23:40', '23:50',
    //     '24'
    // ];
    var xAxisMock = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'];
    // 实时数据
    var realTimeDateMock = [0,732, 816, 924, 840, 912, 684, 816, 1146, 1140, 1116, 1050, 1134, 1230, 1074, 1122, 984, 1074, 966, 894, 918, 900, 972, 738, 948];
    // 平均
    var averageData = [0,726, 828, 906, 852, 924, 726, 798, 1188, 1182, 1080, 1080, 1140, 1218, 1026, 1146, 1014, 1104, 954, 906, 930, 936, 942, 732, 912];  
    
    getMockData();
    setInterval(function() {
        getMockData();
    },600000) 
    
    function getMockData() {
        var currentHour = new Date().getHours().toString();
        var _index = xAxisMock.indexOf(currentHour);
        var new_xAxisMock = xAxisMock.slice(0, _index + 1);
        var new_realTimeDateMock = realTimeDateMock.slice(0, _index + 1);
        var new_averageData = averageData.slice(0, _index + 1);
        var option = {
           legend: {
               data: [
                   { name: '预测', icon: 'rect', }, { name: '实时', icon: 'rect' }],
               textStyle: {
                   color: 'rgba(255,255,255,0.8)'
               },
           },
           tooltip: {
               trigger: 'axis'
           },
            color: ['rgba(43,153,255,0.5)', 'rgba(38,255,255,0.5)'],
           grid: {
               left: '3%',
               right: '4%',
               bottom: '3%',
               containLabel: true
           },
           xAxis:{
                    type: 'category',
                    boundaryGap: false,
                    data: xAxisMock,
                    axisLabel: {
                       textStyle: {
                           color: '#838687',
                           fontSize: '10'
                       },
                    //    interval: 5,
                   },
               },
           yAxis:{
                    type:'value',
                    splitLine: {
                       show: false
                    },
                    axisLabel: {
                        textStyle: {
                           color: '#838687',
                           fontSize: '10'
                        },
                        interval: 0,
                    },
               },
           series: [
               {
                   name: '预测',
                   type: 'line',
                   symbol: 'none',
                   smooth: true,
                   lineStyle: {
                       width: 0,
                       color: 'rgb(43,153,255)'
                   },
                   areaStyle: {
                       normal: {
                           color: 'rgba(43,153,255,0.4)',
                       }
                   },
                   data: averageData
               },
               {
                   name: '实时',
                   type: 'line',
                   symbol: 'none',
                   lineStyle: {
                       width: 0,
                       color: 'rgb(38,255,255)',
                   },
                    areaStyle: {
                        normal: {
                            color: 'rgba(38,255,255,0.4)',
                        }
                    },
                   smooth: true,
                   data: new_realTimeDateMock
               },
           ]
        };
        echarts.init(document.getElementById('energyConsumption')).setOption(option);
    } 

})()